
package net.xfok.ui.drag;

import java.util.List;

import net.xfok.ui.R;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Default launcher application.
 */
public final class Workspace extends Activity implements OnLongClickListener{
	private static final String TAG="Workspace";
	private static final int ITEM_X=4;
	private static final int ITEM_Y=4;
	private static final int ITEM_COUNT=16;

	private Screen mScreen;
	private MenusView mMenus;
	private LinearLayout page;
    private int pageNum,itemCount;
    private DragLayer mDragLayer;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drag_workspace);
        setupViews();
    }


    @Override
    protected void onResume() {
        super.onResume();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> apps=getPackageManager().queryIntentActivities(mainIntent, 0);
		itemCount=apps.size();
		pageNum=itemCount/ITEM_COUNT+1;
        for(int i=0;i<pageNum;i++){
        	addScreen();
        }
        addMenus();
        bindItems(apps);
        addMenus(apps);
    	setPage();
    }

    /**
     * Finds all the views we need and configure them properly.
     */
    private void setupViews() {
    	mDragLayer=(DragLayer)findViewById(R.id.drag_layer);
        page=(LinearLayout)findViewById(R.id.page);
        mScreen = (Screen)findViewById(R.id.workspace);
        mMenus=(MenusView)findViewById(R.id.menus);
        final Screen screen = mScreen;             
        screen.setWorkspace(this); 
        screen.setDragger(mDragLayer);
        screen.setOnLongClickListener(this);
        
        mMenus.setWorkspace(this); 
        mMenus.setDragger(mDragLayer);
        mMenus.setOnLongClickListener(new MenuLongClick());
    }

    public void addScreen(){
    	mScreen.addWorkspace();
    }
    
    public void addMenus(){
    	mMenus.addWorkspace();
    }
    
    public void setPage(){
    	if(page!=null){
	    	page.removeAllViews();
	    	for(int id=0;id<pageNum;id++){
		    	if(id!=mScreen.getCurrentScreen()){
		    		ImageView child=new ImageView(this);     
		            child.setImageResource(R.drawable.page_more);
		            child.setId(id);
		            child.setOnClickListener(new PageListener());
		            page.addView(child);
		    	}else{
		    		TextView child=new TextView(this);
		    		child.setBackgroundResource(R.drawable.page_focus_middle);
		    		child.setGravity(Gravity.CENTER);
		    		child.setText(String.valueOf(id+1));
		    		child.setTextColor(Color.WHITE);
		    		child.setId(id);
		    		child.setOnClickListener(new PageListener());
		            page.addView(child);
		    	}
	    	}
    	}
    }
    
    private class PageListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			mScreen.snapToScreen(v.getId());		
		}	
    }
    
    private void addMenus(List<ResolveInfo> apps){
    	Intent intent;
		final MenusView screen = mMenus;
		int i=0;
		for(ResolveInfo info:apps){	
			if(i>=4){
				break;
			}
    		ComponentName component = new ComponentName(
                    info.activityInfo.applicationInfo.packageName,
                    info.activityInfo.name);
    		intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setComponent(component);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    		int x=i%ITEM_X;
    		int s=i/ITEM_COUNT;
    		int y=i/ITEM_Y-s*ITEM_X;
			Log.i(TAG, "=================> x="+x+" y="+y+" screen="+s);
			final ImageView child=new ImageView(this);
			child.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
//			final ImageView child=new ImageView(this);     
//            child.setImageResource(R.drawable.page_more);
			screen.addInScreen(child, s, x, y, 1, 1,true);
			i++;
    	}
    }
    
    private void bindItems(List<ResolveInfo> apps){
		int i=0;
		Intent intent;
		final Screen screen = mScreen;
    	for(ResolveInfo info:apps){
    		ComponentName component = new ComponentName(
                    info.activityInfo.applicationInfo.packageName,
                    info.activityInfo.name);
    		intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setComponent(component);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    		int x=i%ITEM_X;
    		int s=i/ITEM_COUNT;
    		int y=i/ITEM_Y-s*ITEM_X;
			Log.i(TAG, "=================> x="+x+" y="+y+" screen="+s);
			final ImageView child=new ImageView(this);
			child.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
//			final ImageView child=new ImageView(this);     
//            child.setImageResource(R.drawable.page_more);
			screen.addInScreen(child, s, x, y, 1, 1,true);
			i++;
    	}
    	screen.requestLayout();
	}
    private class MenuLongClick implements OnLongClickListener{

		@Override
		public boolean onLongClick(View v) {
			Log.i(TAG, "=====>onLongClick");
	        if (!(v instanceof CellLayout)) {
	            v = (View) v.getParent();
	        }

	        CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();

	        // This happens when long clicking an item with the dpad/trackball
	        if (cellInfo == null) {
	            return true;
	        }

//	        if (mWorkspace.allowLongPress()) {
	            if (cellInfo.cell == null) {
	                if (cellInfo.valid) {
	                    // User long pressed on empty space
//	                    mScreen.setAllowLongPress(false);
	                }
	            } else {
	                    // User long pressed on an item
//	                    mScreen.startDrag(cellInfo);
	                    mMenus.startDrag(cellInfo);
	            }
//	        }
	        return true;
		}
    	
    }
    public boolean onLongClick(View v) {
    	Log.i(TAG, "=====>onLongClick");
        if (!(v instanceof CellLayout)) {
            v = (View) v.getParent();
        }

        CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();

        // This happens when long clicking an item with the dpad/trackball
        if (cellInfo == null) {
            return true;
        }

//        if (mWorkspace.allowLongPress()) {
            if (cellInfo.cell == null) {
                if (cellInfo.valid) {
                    // User long pressed on empty space
//                    mScreen.setAllowLongPress(false);
                }
            } else {
                    // User long pressed on an item
                    mScreen.startDrag(cellInfo);
//                    mMenus.startDrag(cellInfo);
            }
//        }
        return true;
    }

    Screen getWorkspace() {
        return mScreen;
    }


//	@Override
//	public void onClick(View v) {
//		// TODO Auto-generated method stub
//		
//	}
}
